package org.yx.service.smfr.view_process_path_operation;

import com.xiaoleilu.hutool.util.StrUtil;
import org.dbist.dml.Page;
import org.dbist.dml.Query;
import org.yx.models.SmfrOprDef;
import org.yx.models.SmfrPthOpr;
import org.yx.utils.BaseService;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询某工艺路线的工序
 * @author zxw
 * @since 2018/03/30
**/
public class SmfrViewProcessPathOperationService extends BaseService {
	public SmfrViewProcessPathOperationOut viewProcessPathOperation(SmfrViewProcessPathOperationIn in) throws Exception {
		SmfrViewProcessPathOperationOut out = new SmfrViewProcessPathOperationOut();

		if (!StrUtil.isBlank(in.getPathCode())) {
			Query query = new Query(in.getPageIndex(), in.getPageSize());
			query.addFilter("orgCode", in.getUserOrgCode());
			List<SmfrOprDef> oprDefList = selectList(SmfrOprDef.class, query);
			query.addFilter("pathCode", in.getPathCode());
			query.addOrder("seqNum", true);

			Page<SmfrPthOpr> oprPage = selectPage(SmfrPthOpr.class, query);
			List<SmfrOprDef> oprList = new ArrayList<SmfrOprDef>();
			for (SmfrPthOpr pthOpr : oprPage.getList()) {
				SmfrOprDef oper = new SmfrOprDef();
				copyProperties(pthOpr, oper);
				for (SmfrOprDef oprDef : oprDefList) {
					if (oprDef.getOper().equals(pthOpr.getOper())) {
						oper.setOperDesc(oprDef.getOperDesc());
						break;
					}
				}
				oprList.add(oper);
			}
			out.setOperList(oprList);
			out.setTotalSize(oprPage.getTotalSize());
			out.setProcessPath(in.getPathCode());
		}

		return out;
	}
}